package Test;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import user.com.Fakc;
import user.com.User;

import java.util.List;

public class TestUilt {
    private static Session session = null;
    private static SessionFactory sessionFactory = null;


    @Before
    public void qang() {
        Configuration configuration = new Configuration()
                .configure("hibernate.cfg.xml");
        sessionFactory = configuration.buildSessionFactory();
        this.session = sessionFactory.openSession();

        this.session.beginTransaction();
    }

    @Test
    public void get() {
        Query query = session.createQuery("from User u where 1=1 and u.id=:ags ");
        query.setInteger("ags", 63);
        List<User> list = query.list();
        for (User s : list) {
            System.err.println(s.getName());

            for (Fakc f : s.getAddList()) {
                System.out.print(f.getCol() + " ");

            }
            System.out.println();
        }
    }

    @Test
    public void sa() {
        Criteria criteria = session.createCriteria(User.class);
        List<User> list = criteria.setFirstResult(0).setMaxResults(3).list();
        for (User user : list) {
            System.out.println(user.getName());
        }
        List list1 = criteria.add(Restrictions.in("id", new Integer[]{
                63, 64})).list();

    }

    @Test
    public void set() {
        User user = new User();
        user.setName("零食");
        User user1 = new User();
        user1.setName("辣条");

        Fakc fakc = new Fakc();
        fakc.setCol("鞋底");
        Fakc fakc1 = new Fakc();
        fakc1.setCol("王子");
        user.getAddList().add(fakc);
        user.getAddList().add(fakc1);
        user1.getAddList().add(fakc);
        user1.getAddList().add(fakc1);
        session.save(user);
        session.save(user1);
        session.save(fakc);
        session.save(fakc1);


    }

    @Test
    public void data() {
        User user = (User) session.get(User.class, 63);
        Fakc fakc = (Fakc) session.get(Fakc.class, 64);
        user.getAddList().add(fakc);
        session.save(user);
        System.out.println(fakc.getCol());

    }

    @Test
    public void s() {
        Query query = session.createQuery("from User u where 1=1 and u.id=:ags ");

    }

    @After
    public void Sass() {
        this.session.getTransaction().commit();
        this.session.clear();
        this.sessionFactory.close();

    }

}
